/*
 * @Author: your name
 * @Date: 2021-09-03 11:26:07
 * @LastEditTime: 2021-10-24 16:21:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /柳橙/拆单红冲/yongyou_zj5j_separatebill/src/main.js
 */
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import VueRouter from 'vue-router';
import baseRoutes from '/@/router/base';
import Route from '/@/router/index';
import store from '/@/store';
import ElementUI from 'element-ui';
import Api from '@/api/apiFetch'
import 'element-ui/lib/theme-chalk/index.css';

import message from '/@/components/messageBox/messageBox.js';
Vue.use(message);
// css样式文件
import '/@/assets/css/export.scss';
import '/@/styles/index.scss';
import App from '/@/App.vue';

import Tools from '/@/util/tools';

import Util from '/@/util/util';
import * as filters from '/@/filters'
// import axios from 'axios';
// axios.defaults.baseURL = '/api';

import Rsa from './util/rsa.js';
import 'xe-utils'
import 'exceljs'
import 'xlsx'
import VXETable from 'vxe-table';
import VXETablePluginExportXLSX from 'vxe-table-plugin-export-xlsx'
import 'vxe-table/lib/style.css';
VXETable.use(VXETablePluginExportXLSX);
Vue.use(VXETable);

// 引入Echarts
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts;

Vue.prototype.Rsa = Rsa; // 将Rsa注册为公共方法,方便其他页面调用

Vue.config.productionTip = false;
Vue.use(ElementUI);

// 初始化路由
Vue.use(VueRouter);
Route.routes = baseRoutes;
let routes = Route.initRoutes();
export const router = new VueRouter({
  routes,
});

try {
  Tools({
    Vue,
  });
} catch (e) { }
// 百度编辑器上传图片地址
localStorage.setItem('uploadUrl', Util.uploadURL());


// 路由跳转前
router.beforeEach((to, from, next) => {
  let userInfo = localStorage.getItem('userInfo');

  // let LCPath = to.path;
  let userId = ''
  if (process.env.NODE_ENV === 'development') {
    userId = 'd2aad9f5-c9f0-4d10-94be-dc84dd6c9009'
  }
  if (!userInfo) {
    Api.loginSsoFetch({ userId }).then((res) => {
      console.log(res, 'res2');
      localStorage.setItem("userInfo", JSON.stringify(res.data));
      localStorage.setItem("sessionId", res.data.sessionId);
      next()
    });
  } else {
    try {
      window.jDiwork.getContext(data => {
        const { userId } = JSON.parse(userInfo)
        if (data.userid !== userId) {
          Api.loginSsoFetch({ userId: data.userid }).then((res) => {
            console.log(res, 'res2');
            localStorage.setItem("userInfo", JSON.stringify(res.data));
            localStorage.setItem("sessionId", res.data.sessionId);
            next()
          });
          return next();
        }
        console.log('jDiwork', data.userid)
      });
    } catch (e) {
      console.log(e)
      return next()
    }
    next()
  }
});
// register global utility filters
// console.log(filters)
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');
